#include "printerinfodialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "dataprovider.h"

PrinterInfoDialog::PrinterInfoDialog(QWidget *parent)
    : QDialog{parent}
{
    setupUI();
}

void PrinterInfoDialog::setupUI() {
    setWindowTitle("好用得效期系统");
    setFixedSize(QSize(300, 200));
    auto dataprovider = DataProvider::instance();
    auto shop = dataprovider->GetShopInfo();

    titleLabel = new QLabel("打印机信息");

    // 布局管理
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(titleLabel);
    mainLayout->addSpacing(10);

    QHBoxLayout *lineLayout = new QHBoxLayout;
    auto nameTitle = new QLabel("名称：");
    nameTitle->setFixedWidth(80);
    nameTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    nameLabel = new QLabel(shop->PrinterName);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    lineLayout->addWidget(nameTitle);
    lineLayout->addWidget(nameLabel);
    mainLayout->addLayout(lineLayout);

    lineLayout = new QHBoxLayout;
    auto ipTitle = new QLabel("Ip地址：");
    ipTitle->setFixedWidth(80);
    ipTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    ipLabel = new QLabel(shop->PrinterIp);
    ipLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    lineLayout->addWidget(ipTitle);
    lineLayout->addWidget(ipLabel);
    mainLayout->addLayout(lineLayout);

    lineLayout = new QHBoxLayout;
    auto portTitle = new QLabel("端口：");
    portTitle->setFixedWidth(80);
    portTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    portLabel = new QLabel(shop->PrinterPort);
    portLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    lineLayout->addWidget(portTitle);
    lineLayout->addWidget(portLabel);
    mainLayout->addLayout(lineLayout);

    mainLayout->addSpacing(10);

    confirmButton = new QPushButton("确认");
    // cancelButton = new QPushButton("取消");
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(confirmButton);
    // buttonLayout->addWidget(cancelButton);

    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    applyStyles();

    qDebug() << "Create Password Dialog ok....";

    connect(confirmButton, &QPushButton::clicked, this, &PrinterInfoDialog::onConfirm);
    // connect(cancelButton, &QPushButton::clicked, this, &PrinterInfoDialog::exitApplication);

}

void PrinterInfoDialog::applyStyles(){
    setStyleSheet(R"(
        QDialog { background: #f0f2f5; }
        QLabel { font: 18px bold; color: #333; }
        QLineEdit {
            padding:8px;
            border:1px solid #ddd;
            border-radius:4px;
            font: 14px;
        }
        QPushButton {
          padding:10px 20px;
          border:2px solid #ddd;
          border-radius:4px;
          font:bold 14px;
        }
        QPushButton[text="登录"] { background:#4890ee; color:white; height: 40px; font: 24px bold;}
        QPushButton[text="登录"]:hover { background:#40a9ff; }
        QPushButton[text="退出"] { background:#ff6666; color:white; height: 40px; font: 24px bold;}
        )"
                  );

    titleLabel->setStyleSheet("font: bold 28px; color: #1a73e8;");
    titleLabel->setAlignment(Qt::AlignCenter);
}


void PrinterInfoDialog::onConfirm(){
    disconnect();
    close();
}

void PrinterInfoDialog::exitApplication() {
    disconnect();
    close();
}
